﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class ClassHourDAO : CoreAbstractDAO<ClassHour>
    {
        private AdapterAbstractDAO<ClassHour, ClassHourSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<ClassHour, ClassHourUniversity> wrappedAbstractDAOUniversity;
        private ClassHourDAOSchool wrappedDAOSchool;
        private ClassHourDAOUniversity wrappedDAOUniversity;

        public ClassHourDAO(ClassHourDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool = 
                new AdapterAbstractDAO<ClassHour,ClassHourSchool>((AbstractDAO<ClassHourSchool>)wrappedDAOSchool);
        }

        public ClassHourDAO(ClassHourDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<ClassHour, ClassHourUniversity>((AbstractDAO<ClassHourUniversity>)wrappedDAOUniversity);
        }

        public IList<ClassHour> getClassHoursFromInterval(Hour hour)
        {
            if (wrappedDAOSchool != null)
            {
                List<ClassHourSchool> list = (List<ClassHourSchool>)wrappedDAOSchool.getClassHoursFromInterval(hour);
                return list.ConvertAll(x => (ClassHour)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<ClassHourUniversity> list = (List<ClassHourUniversity>)wrappedDAOUniversity.getClassHoursFromInterval(hour);
                return list.ConvertAll(x => (ClassHour)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void update(ClassHour entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            { 
                wrappedAbstractDAOUniversity.update(entity); 
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void save(ClassHour entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(ClassHour entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override void delete(ClassHour entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }
        }

        public override ClassHour getById(int id)
        {
            ClassHour entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<ClassHour> getAll()
        {
            IList<ClassHour> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassHourDAOSchool nor ClassHourDAOUniversity was initialized");
            }

            return records;
        }
    }
}
